package dbops

import (
	"gitee.com/DengAnbang/Computation/src/bean"
	"database/sql"
	"gitee.com/DengAnbang/goutils/dbutils"
	"gitee.com/DengAnbang/goutils/loge"
	"gitee.com/DengAnbang/goutils/utils"
)

func AddTemplateNode(treeNodeId, no, name, unit, designNum, price, remarks string) (id string, err error) {
	if len(treeNodeId) == 0 {
		id = ""
		err = bean.NewErrorMessage("树节点ID为空")
		return
	}
	if len(no) == 0 {
		id = ""
		err = bean.NewErrorMessage("序号为空")
		return
	}
	if len(name) == 0 {
		id = ""
		err = bean.NewErrorMessage("名字为空")
		return
	}
	if len(unit) == 0 {
		id = ""
		err = bean.NewErrorMessage("单位为空")
		return
	}
	if len(designNum) == 0 {
		id = ""
		designNum = ""
		//err = bean.NewErrorMessage("设计量为空")
		//return
	}
	if len(price) == 0 {
		id = ""
		price = ""
		//err = bean.NewErrorMessage("价格为空")
		//return
	}

	id = utils.NewUUID()
	stmtIn, err := dbConn.Prepare("INSERT INTO table_tree_template (template_id,tree_node_id,no,name,unit,design_num,price, remarks)VALUES(?,?,?,?,?,?,?,?)")
	if err != nil {
		return
	}
	_, err = stmtIn.Exec(id, treeNodeId, no, name, unit, designNum, price, remarks)
	defer stmtIn.Close()
	return
}
func AmendTemplateNode(templateId, treeNodeId, no, name, unit, designNum, price, remarks string) (err error) {
	if len(treeNodeId) == 0 {
		err = bean.NewErrorMessage("树节点ID为空")
		return
	}
	if len(no) == 0 {
		err = bean.NewErrorMessage("序号为空")
		return
	}
	if len(name) == 0 {
		err = bean.NewErrorMessage("序号为空")
		return
	}
	if len(unit) == 0 {
		err = bean.NewErrorMessage("单位为空")
		return
	}
	if len(designNum) == 0 {
		err = bean.NewErrorMessage("设计量为空")
		return
	}
	if len(price) == 0 {
		err = bean.NewErrorMessage("价格为空")
		return
	}
	stmtIn, err := dbConn.Prepare("UPDATE  table_tree_template SET tree_node_id=?,no=?,name=?,unit=?,design_num=?,price=?, remarks=? WHERE template_id=?")
	if err != nil {
		return
	}
	_, err = stmtIn.Exec(treeNodeId, no, name, unit, designNum, price, remarks, templateId)
	defer stmtIn.Close()
	return
}

func SelectTemplateList(nodeId string) ([]bean.TemplateBean, error) {
	if len(nodeId) == 0 {
		return nil, bean.NewErrorMessage("节点ID不能为空")
	}
	var templateList = make([]bean.TemplateBean, 0, 8)
	var err error
	var stmtOut *sql.Stmt
	stmtOut, err = dbConn.Prepare("SELECT * FROM table_tree_template WHERE tree_node_id = ?")
	if err != nil {
		return templateList, err
	}
	defer stmtOut.Close()
	rows, err := stmtOut.Query(nodeId)
	if err != nil {
		return templateList, err
	}
	defer rows.Close()
	for rows.Next() {
		var strings map[string]string
		strings, err = dbutils.GetRowsMap(rows)
		if err == nil {
			tree := bean.TemplateBean{}.SetData(strings)
			templateList = append(templateList, tree)
		} else {
			loge.W(err)
		}

	}
	//loge.DE(bean.NewSucceedMessage(code.TypeTemplateSelect, templateList).GetJson())
	return templateList, err
}

func DeleteTemplateByNode(nodeId string) (err error) {
	if len(nodeId) == 0 {
		return bean.NewErrorMessage("删除的节点ID不能为空")
	}
	stmtIn, err := dbConn.Prepare("DELETE FROM table_tree_template WHERE tree_node_id=?")
	if err != nil {
		return
	}
	_, err = stmtIn.Exec(nodeId)
	stmtIn.Close()
	return
}
func DeleteTemplateByID(templateId string) (err error) {
	if len(templateId) == 0 {
		return bean.NewErrorMessage("模板ID为空")
	}
	stmtIn, err := dbConn.Prepare("DELETE FROM table_tree_template WHERE template_id=?")
	if err != nil {
		return
	}
	_, err = stmtIn.Exec(templateId)
	stmtIn.Close()
	return
}
